<textarea id="upload-editor-view" style="display: none;" data-url="{:url('ueditor/index',['path_type' => !empty($editor_path_type) ? $editor_path_type : 'common'])}"></textarea>
<script type="text/javascript" charset="utf-8" src="__ADMIN_PATH__/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__ADMIN_PATH__/ueditor/ueditor.all.js"> </script>
<script type="text/javascript" charset="utf-8" src="__ADMIN_PATH__/ueditor/lang/zh-cn/zh-cn.js"></script>

<script type="text/javascript">
	var upload_editor;
	layui.use(["element", "form", "laydate", "colorpicker"], function () {
		let $ = layui.jquery;
		upload_editor = UE.getEditor('upload-editor-view',{
	        isShow: false,
	        focus: false,
	        enableAutoSave: false,
	        autoSyncData: false,
	        autoFloatEnabled:false,
	        wordCount: false,
	        sourceEditor: null,
	        scaleEnabled:true,
	        toolbars: [["insertimage", "insertvideo", "attachment"]],
	        serverUrl: $('#upload-editor-view').data('url')
	    });
	    
	    //点击进行上传
	    $(document).on('click', '.plug-file-upload-submit', function(){
			// 组件是否初始化
	    	if(typeof(upload_editor) != 'object')
	    	{
	    		layer.msg("组件未初始化", {icon: 7, time: 2000});
	            return false;
	    	}
	    	// 容器是否指定
	        if(($(this).attr('data-view-tag') || null) == null)
	        {
	            layer.msg("未指定容器", {icon: 7, time: 2000});
	            return false;
	        }
	        // 容器
        	var $view_tag = $($(this).attr('data-view-tag'));
        	// 加载组建类型
        	var dialog_type = null;
        	switch($view_tag.attr('data-dialog-type'))
	        {
	            // 视频
	            case 'video' :
	                dialog_type = 'insertvideo';
	                break;
	            // 图片
	            case 'images' :
	                dialog_type = 'insertimage';
	                break;
	            // 文件
	            case 'file' :
	                dialog_type = 'attachment';
	                break;
	        }
	        if(dialog_type == null)
	        {
	            layer.msg("未指定加载组建", {icon: 7, time: 2000});
	            return false;
	        }
	        // 是否指定form名称
	        if(($view_tag.attr('data-form-name') || null) == null)
	        {
	            layer.msg("未指定表单name名称", {icon: 7, time: 2000});
	            return false;
	        }
	        // 打开组建
        	var dialog = upload_editor.getDialog(dialog_type);
        	dialog.render();
        	dialog.open();

        	// 赋值参数
        	$('body').attr('view-tag',$(this).attr('data-view-tag'));
		});

		// 监听多图上传和上传附件组件的插入动作
		if(typeof(upload_editor) == 'object')
		{
			upload_editor.ready(function(){
				// 图片上传动作
				upload_editor.addListener("beforeInsertImage", function(t, result){
					if(result.length > 0)
					{
						var $tag = $($('body').attr('view-tag'));
						var max_number = $tag.attr('data-max-number') || 0;
						var is_delete = ($tag.attr('data-delete') == undefined) ? 1 : $tag.attr('data-delete');
						var form_name = $tag.attr('data-form-name') || '';
						var is_attr = $tag.attr('data-is-attr') || null;
						// 只限制一条
		                if(max_number <= 1)
		                {
		                    $tag.find('li').remove();
		                }
		                // 循环处理
		                for(var i in result)
		                {
		                	// 是否直接赋值属性
		                	if(i == 0 && is_attr != null)
		                	{
		                		$('form [name="'+form_name+'"]').val(result[i].src);
		                		$tag.attr(is_attr, result[i].src);
		                		break;
		                	}

		                	// 是否限制数量
		                    if(max_number > 0 && $tag.find('li').length >= max_number)
		                    {
		                        layer.msg("最多上传"+max_number+"张图片", {icon: 7, time: 2000});
		                        break;
		                    }

		                    var html = '<li>';
		                        html += '<input type="hidden" name="'+form_name+'" value="'+result[i].src+'" />';
		                        html += '<img src="'+result[i].src+'" height="100" />';
		                        if(is_delete == 1)
		                        {
		                        	html += '<i class="layui-icon title-nav-remove">&#x1006;</i>';
		                        }
		                        html += '</li>';
		                    $tag.append(html);
		                }
					}
				});

				// 视频上传
		        upload_editor.addListener("beforeInsertVideo", function(t, result)
		        {
		            if(result.length > 0)
		            {
		                var $tag = $($('body').attr('view-tag'));
		                var max_number = $tag.attr('data-max-number') || 0;
		                var is_delete = ($tag.attr('data-delete') == undefined) ? 1 : $tag.attr('data-delete');
		                var form_name = $tag.attr('data-form-name') || '';
		                var is_attr = $tag.attr('data-is-attr') || null;

		                // 只限制一条
		                if(max_number <= 1)
		                {
		                    $tag.find('li').remove();
		                }

		                // 循环处理
		                for(var i in result)
		                {
		                	// 是否直接赋值属性
		                	if(i == 0 && is_attr != null)
		                	{
		                		$('form [name="'+form_name+'"]').val(result[i].url);
		                		$tag.attr(is_attr, result[i].url);
		                		break;
		                	}

		                	// 是否限制数量
		                    if(max_number > 0 && $tag.find('li').length >= max_number)
		                    {
		                        layer.msg("最多上传"+max_number+"个视频", {icon: 7, time: 2000});
		                        break;
		                    }

		                    var $tag = $($('body').attr('view-tag'));
		                    var html = '<li>';
		                        html += '<input type="hidden" name="'+form_name+'" value="'+result[i].url+'" />';
		                        html += '<video src="'+result[i].url+'" width="300" controls>your browser does not support the video tag</video>';
		                        if(is_delete == 1)
		                        {
		                        	html += '<i class="layui-icon title-nav-remove">&#x1006;</i>';
		                        }
		                        html += '</li>';
		                    $tag.append(html);
		                }
		            }
		        });

		        //文件上传动作
		        upload_editor.addListener("beforeInsertFile", function(t, result){
		        	if(result.length > 0)
		        	{
		        		var $tag = $($('body').attr('view-tag'));
		        		var max_number = $tag.attr('data-max-number') || 0;
		        		var is_delete = ($tag.attr('data-delete') == undefined) ? 1 : $tag.attr('data-delete');
		        		var form_name = $tag.attr('data-form-name') || '';
		        		var is_attr = $tag.attr('data-is-attr') || null;
		        		// 只限制一条
		                if(max_number <= 1)
		                {
		                    $tag.find('li').remove();
		                }
		                // 循环处理
		                for(var i in result)
		                {
		                	// 是否直接赋值属性
		                	if(i == 0 && is_attr != null)
		                	{
		                		$('form [name="'+form_name+'"]').val(result[i].url);
		                		$tag.attr(is_attr, result[i].url);
		                		break;
		                	}

		                	// 是否限制数量
		                    if(max_number > 0 && $tag.find('li').length >= max_number)
		                    {
		                        layer.msg("最多上传"+max_number+"文件", {icon: 7, time: 2000});
		                        break;
		                    }

		                    var html = '<li>';
		                        html += '<input type="hidden" name="'+form_name+'" value="'+result[i].url+'" />';
		                        html += result[i].url;
		                        if(is_delete == 1)
		                        {
		                        	html += '<i class="layui-icon title-nav-remove">&#x1006;</i>';
		                        }
		                        html += '</li>';
		                    $tag.append(html);
		                }
		        	}
		        });
			});
		}

		// 删除容器中的内容(删除图片)
	    $(document).on('click', '.plug-file-upload-view li i', function()
	    {
	        // 容器
	        var $tag = $(this).parents('ul.plug-file-upload-view');
	        // 删除数据
	        $(this).parent().remove();
	        // 数据处理
	        var max_number = $tag.attr('data-max-number') || 0;
	        if(max_number > 0)
	        {
	            if($tag.find('li').length < max_number)
	            {
	                $('.plug-file-upload-submit').show();
	            }
	        }
	    });
	});
</script>